package com.company.cloud.company.modules.mgr.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.company.modules.api.request.*;
import com.company.cloud.company.modules.api.response.CompanyFull;
import com.company.cloud.company.modules.app.controller.req.CompanyQuery4PageParam;
import com.company.cloud.company.modules.mgr.service.CompanyMgrService;
import com.company.cloud.user.modules.base.entity.DeptInfo;
import com.company.cloud.user.modules.cache.models.DeptModel;
import com.company.cloud.user.modules.mgr.controller.req.DeptSavaParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/user/company")
@Api(tags="CompanyMgr_公司管理")
public class CompanyMgrController {

    @Autowired
    private CompanyMgrService companyMgrService;



    @PostMapping("/query")
    @ApiOperation(value="公司查询", response= CompanyFull.class)
    public BaseResult query(@RequestBody CompanyQuery4PageParam params) {
        CompanyQueryRequest request=new CompanyQueryRequest();
        request.setCompanyNo(params.getCompanyNo());
        request.setName(params.getName());
        request.setType(params.getType());
        request.setSourceType(params.getSourceType());
        request.setSource(params.getSource());
        request.setPage(params.getPage());
        request.setRows(params.getRows());
        request.setState(params.getState());
        IPage<CompanyFull> data= companyMgrService.query(request);
        return BaseResult.ok(data);
    }



    @PostMapping("/addCompany")
    @StaffLogc(title = "CompanyMgr_公司管理 、新增公司", action = "CompanyMgr_公司管理、新增公司", actionAdo = "CompanyMgr_公司管理")
    @ApiOperation(value="新增公司",response = BaseResult.class )
    public BaseResult addCompany(@RequestBody CompanyAddRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        if(!"PTC".equals(userInfo.getCompanyNo())){
            throw new BusinessException(BaseReturnEnum.NOT_AUTHC);
        }
        companyMgrService.save(params,userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/addCompany2User")
    @StaffLogc(title = "CompanyMgr_公司管理 、新增公司和用户", action = "CompanyMgr_公司管理、新增公司和用户", actionAdo = "CompanyMgr_公司管理")
    @ApiOperation(value="新增公司和用户",response = BaseResult.class )
    public BaseResult addCompany2User(@RequestBody Company2UserAddRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        if(!"PTC".equals(userInfo.getCompanyNo())){
            throw new BusinessException(BaseReturnEnum.NOT_AUTHC);
        }
        companyMgrService.save2user(params,userInfo);
        return BaseResult.ok();
    }

    @PostMapping("/updateCompany")
    @StaffLogc(title = "CompanyMgr_公司管理 、更新公司", action = "CompanyMgr_公司管理、更新公司", actionAdo = "CompanyMgr_公司管理")
    @ApiOperation(value="更新公司",response = BaseResult.class )
    public BaseResult updateCompany(@RequestBody CompanyUpdateRequest params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
        if(!"PTC".equals(userInfo.getCompanyNo())){
            throw new BusinessException(BaseReturnEnum.NOT_AUTHC);
        }
        companyMgrService.update(params,userInfo);
        return BaseResult.ok();
    }



//
//    @Override
//    public BaseClientResult<CompanyFull> delCompany(CompanyDelRequest request) {
//        try {
//            ValidatorUtils.validate(request);
//            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
//            userInfo.setId(request.getOpUserId());
//            userInfo.setIp(request.getOpIp());
//            CompanyFull data=companyMgrService.del(request.getCompanyNo(),userInfo);
//            return BaseClientResult.success(data);
//        }catch  (Exception e){
//            return BaseClientResult.error(e);
//        }
//    }


}
